﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using EntityModels;
using BusinessLayer;

namespace PresentationLayer
{
    public partial class Default : AbstractPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //#######################
            Session.Clear();
            //#######################

            try
            {
                HttpCookie cookie = Request.Cookies["eventnet"];
                if (cookie != null)
                {
                    String uname = cookie["eventnet-username"];
                    User u = Controller.Instance.GetUser(uname);
                    if (u != null)
                    {
                        Response.Redirect("Profile.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                debugMessage.InnerText = ex.ToString();
            }

            if (Request.Form.Count > 0)
            {
                string username = "";
                string password = "";
                NameValueCollection nvc = Request.Form;

                if (!string.IsNullOrEmpty(nvc["username_login"]) && !string.IsNullOrEmpty(nvc["password_login"]))
                {
                    username = nvc["username_login"];
                    password = nvc["password_login"];
                    Login(username, password);
                }
            }            
        }

        protected void Login(string username, string password)
        {
            try
            {
                User us;

                if ((username != "") && (password != ""))
                {

                    us = new User();
                    us.username = username;
                    us.password = password;

                    Controller.Instance.LogIn(us);


                    HttpCookie cookie = new HttpCookie("eventnet");
                    Response.Cookies.Add(cookie);
                    cookie["eventnet-username"] = username;
                    cookie.Expires = DateTime.Now.AddMonths(1);

                    Response.Redirect("Profile.aspx", false);
                }
                else
                {
                    throw new Exception("All fields must be filled");

                }
            }
            catch (Exception ex)
            {
                this.debugMessage.InnerHtml += ex.Message;
            }
        }


        //protected void Login(object sender, EventArgs e)
        //{
        //    try
        //    {
               
        //        User us;

        //        if ((username_login.Text != "") && (password_login.Text != ""))
        //        {

        //            us = new User();
        //            us.Username = username_login.Text;
        //            us.Password = password_login.Text;

        //            Controller.Instance.LogIn(us);


        //            HttpCookie cookie = new HttpCookie("eventnet");
        //            Response.Cookies.Add(cookie);
        //            cookie["eventnet-username"] = username_login.Text;
        //            cookie.Expires = DateTime.Now.AddMonths(1);

        //            Response.Redirect("Profile.aspx");
        //        }
        //        else {
        //            throw new Exception("All fields must be filled");
                
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        this.debugMessage.InnerHtml += ex.Message;
        //    }
        //}
      
        protected void Create(object sender, EventArgs e)
        {
            try
            {
                if ((password.Text != "") && (password.Text == pconfirm.Text))
                {
                    User u = new User(username.Text,password.Text,firstname.Text,lastname.Text,email.Text);                   

                    Controller.Instance.RegisterUser(u);
                }
                else
                {
                    this.debugMessage.InnerHtml += "Password does not match. Please try again. <br />";
                }
            }
            catch (Exception ex)
            {
                this.debugMessage.InnerHtml += ex.Message;
            }
        }
    }
}